package com.lunwen.view;

import com.lunwen.dao.ArticleDao;
import com.lunwen.domain.Article;
import com.lunwen.view.component.Context;
import com.lunwen.view.component.MassageTip;
import com.lunwen.view.value.ColorValue;
import com.lunwen.view.value.SizeValue;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;

public class UpdateArticleFrame extends Context implements KeyListener, ActionListener {

    private JTextField search = new JTextField(10);

    private JTextField sid = new JTextField(10);
    private JTextField article_name = new JTextField(10);
    private JTextField article_type = new JTextField(10);
    private JPanel article_content = new JPanel();
    private JTextArea content = new JTextArea(13,42);

    private JButton left = new JButton("<");
    private JButton right = new JButton(">");

    private JButton mod = new JButton("确认修改");

    private ArticleDao dao = new ArticleDao();

    private List<Article> cache = null;
    private int indexOfCache = 0;

    public UpdateArticleFrame(int width, int height) {
        super(width, height);

        search.setBounds(60,0,width-120,40);
        this.add(search,"查找",100);
        search.setFocusable(true);
        search.addKeyListener(this);

        sid.setBounds(60,80,width-120,40);
        article_name.setBounds(60,130,width-120,40);
        article_type.setBounds(60,180,width-120,40);

        article_content.setBounds(60,230,width-120,250);

        this.add(sid,"作者学号",100);
        this.add(article_name,"论文题目",100);
        this.add(article_type,"论文类型",100);

        JLabel label = new JLabel("论文的详细内容");
        label.setBounds(0,0,width-120,48);
        label.setFont(new Font("幼圆", Font.BOLD, SizeValue.ContextFont));
        label.setForeground(ColorValue.ContextForeground);
        article_content.add(label);

        content.setLineWrap(true);
        content.setEditable(true);

        JScrollPane scrollpane=new JScrollPane(content);
        scrollpane.setBounds(5,48,470,195);
        article_content.add(scrollpane);
        content.setForeground(ColorValue.ContextForeground);
        article_content.setBackground(ColorValue.ContextBackground);
        article_content.setForeground(Color.GRAY);
        article_content.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, ColorValue.Border));

        this.add(article_content);


        left.setBounds(0,(height-40)/2,40,40);
        this.add(left,20);
        right.setBounds(width-40,(height-40)/2,40,40);
        this.add(right,20);

        mod.setBounds(60,480,width-120,48);
        this.add(mod,20);

        left.addActionListener(this);
        right.addActionListener(this);
        mod.addActionListener(this);

    }

    public void setValue(Article article, String id){
        sid.setText(id);
        article_name.setText(article.getName());
        article_type.setText(article.getType());
        content.setText(article.getContent());
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

        if (e.getKeyChar() == KeyEvent.VK_ENTER){
            cache = dao.find(search.getText());
            indexOfCache = 0;
            if (cache != null && cache.size() != 0){
                Article article = cache.get(indexOfCache);
                String sid = dao.findSidByBid(article.getAid());
                setValue(article,sid);
                MassageTip.showMassage(null,"找到"+cache.size()+"个");
            }else {
                MassageTip.showMassage(null,"没有找到");
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (cache != null){
            if (e.getSource() == left){
                if (indexOfCache > 0){
                    indexOfCache --;
                    Article article = cache.get(indexOfCache);
                    String sid = dao.findSidByBid(article.getAid());
                    setValue(article,sid);
                }else{
                    MassageTip.showMassage(null,"前面再没有了");
                }
            }else if (e.getSource() == right){
                if (indexOfCache < cache.size()-1){
                    indexOfCache ++;
                    Article article = cache.get(indexOfCache);
                    String sid = dao.findSidByBid(article.getAid());
                    setValue(article,sid);
                }else {
                    MassageTip.showMassage(null,"后面再没有了");
                }
            }else if (e.getSource() == mod){
                dao.update(cache.get(indexOfCache).getAid(),content.getText());
                MassageTip.showMassage(null,"修改成功");
            }
        }

//        if (e.getSource() == mod){
//
//            if (!StringUtils.isEmpty(sid.getText())){
//                if (!StringUtils.isEmpty(article_name.getText())){
//
//                    if (!StringUtils.isEmpty(article_type.getText())){
//                        if (!StringUtils.isEmpty(ind.getText())
//                                && (ind.getText().equals("管理员") || ind.getText().equals("学生"))){
//                            if (!StringUtils.isEmpty(state.getText())){
//                                Person person = new Person();
//                                person.setId(id.getText());
//                                person.setName(name.getText());
//                                person.setPassword(pass.getText());
//                                person.setInd(ind.getText());
//                                person.setState(state.getText());
//                                if (!StringUtils.isEmpty(money.getText())){
//                                    person.setMoney(Integer.parseInt(money.getText()));
//                                }else {
//                                    person.setMoney(0);
//                                }
//                                int update = dao.update(person);
//                                if (update > 0){
//                                    MassageTip.showMassage(null,"修改成功");
//                                }else if(update == 0){
//                                    MassageTip.showMassage(null,"学号不可修改");
//                                }else{
//                                    MassageTip.showMassage(null,"修改失败");
//                                }
//                            }else{
//                                MassageTip.showMassage(null,"状态不能为空");
//                            }
//                        }else{
//                            MassageTip.showMassage(null,"身份错误");
//                        }
//                    }else{
//                        MassageTip.showMassage(null,"密码不能为空");
//                    }
//
//                }else{
//                    MassageTip.showMassage(null,"名称不能为空");
//                }
//            }else{
//                MassageTip.showMassage(null,"账号不能为空");
//            }
//        }
    }
}